﻿
/**
*请求变更/变更确认
*/
Chenxin.UI.ChangeConfirmationForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/ConfirmRequestChange.ashx',
    id: 'changeConfirmationWin',
    renderTo: 'jqm_changeconfirmation',
	defBBtn:true,
    initFormValidator: function() {
        _this = this;
        $("#requestChangePersonId").formValidator({ validatorgroup: this.id, onshow: "请输入工号", onfocus: "输入工号", oncorrect: "输入正确" }).inputValidator({ //数字验证
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "" }, onerror: ""
        }).ajaxValidator({
            url: "service/ConfirmRequestChange.ashx",
            datatype: "json",
            data: "houseid=" + _this.currentId + "&op=validate",
            success: function(data) {
                if (data.success == 0) {
                    return false;
                } else {
                    _this.requestUser = data.data.model;

                    if (data.success == 0) {
                        $('#request_name').val('');
                        $('#request_department').val('');
                        return false;
                    } else {
                        $('#request_name').val(data.data.model.UserName);
                        $('#request_department').val(data.data.model.DepartName);
                        return true;
                    }
                }
            },
            buttons: $("#changeconfirmation_Save"),
            error: function() { alert("服务器没有返回数据，可能服务器忙，请重试"); },
            onerror: "员工号不存在",
            onwait: "正在校验，请稍候..."
        });
        $("#requestChangeTxtSummary").formValidator({ validatorgroup: this.id,
            onshow: "请输入请求说明！", onfocus: "请输入请求说明", oncorrect: " "
        }).inputValidator({
            min: 1, empty: { emptyerror: "请求说明不能为空！" }, onerror: "请求说明不能为空！"
        });

        $("#request_customerName").formValidator({ validatorgroup: this.id,
            onshow: "请输入客户姓名！", onfocus: "请输入客户姓名", oncorrect: "输入正确"
        }).inputValidator({
            min: 1, empty: { emptyerror: "客户姓名不能为空！" }, onerror: "客户姓名不能为空！"
        });
        //联系电话
        $("#request_customerPhone").formValidator({ validatorgroup: this.id,
            onshow: "请输入电话！", onfocus: "请输入电话", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#request_customerPhone").val() == "" && $("#request_customerMoblie").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话！"
        });
        //手机号码
        $("#request_customerMoblie").formValidator({ validatorgroup: this.id,
            onshow: "请输入手机号！", onfocus: "请输入手机号", oncorrect: " "
        }).functionValidator({
            fun: function(val, elem) { if (val == "") return true; return val.isMobile(); }, onerror: "请输入正确的手机号"
        }).functionValidator({
            fun: function(val, elem) {
                if ($("#request_customerPhone").val() == "" && $("#request_customerMoblie").val() == "") {
                    return false;
                } else {
                    return true;
                }
            }, onerror: "请输入手机号码或电话！"
        });
    },
    createForm: function() {
        return Chenxin.FormCommon.houseChangeConfirmationForm('changeconfirmation');
    },
    createComboList: function() {
        this.contactType = new Chenxin.UI.Combo({
            name: 'HouseStatus',
            containerId: '#changeconfirmation_requestChangeStyle',
            key: 'House.Status',
            required: true,
            items: comboCommon.houseStatus
        });

        return [this.contactType];
    },
    initEvent: function() {
        var _this = this;
        $('#changeconfirmation_Save').live('click', function() {
            _this.save();
        });
        $('#changeconfirmation_Cancle').live('click', function() {
            _this.hide();
        });
        $('#changeConfirmationWin input:radio[name="ChangeType"]').live('click', function() {
            if (this.value == '10') {//状态变更
                $("#changeconfirmation_requestChangeStyle").removeAttr("disabled"); //.attr("disabled", false);
                $("#requestChangePersonId").attr("disabled", "disabled");
                $("#requestChangePersonId").val("");
                $("#request_customerName").val("");
                $("#request_customerPhone").val("");
                $("#request_customerMoblie").val("");
                $("#request_name").val("");
                $("#request_department").val("");
                $("#request_customerName").attr("disabled", "disabled");
                $("#request_customerPhone").attr("disabled", "disabled");
                $("#request_customerMoblie").attr("disabled", "disabled");
                $("#request_tr1").show();
                $("#request_tr2").hide();
                $("#request_tr3").hide();
                $("#request_tr4").hide();
                $("#request_tr5").hide();
                $("#request_tr6").hide();
                $("#request_tr7").hide();
            //    _this.contactType.setEnable();
            } else if (this.value == '20') {//所属人变更
                $("#changeconfirmation_requestChangeStyle").attr("disabled", "disabled");
                $("#requestChangePersonId").removeAttr("disabled");
                $("#request_customerName").removeAttr("disabled");
                $("#request_customerPhone").removeAttr("disabled");
                $("#request_customerMoblie").removeAttr("disabled");
                $("#request_tr1").hide();
                $("#request_tr2").show();
                $("#request_tr3").show();
                $("#request_tr4").show();
                $("#request_tr5").show();
                $("#request_tr6").show();
                $("#request_tr7").show();
              //  _this.contactType.setDisable();
            } else if (this.value == '30') {
                $("#changeconfirmation_requestChangeStyle").removeAttr("disabled");
                $("#requestChangePersonId").removeAttr("disabled");
                $("#request_customerName").removeAttr("disabled");
                $("#request_customerPhone").removeAttr("disabled");
                $("#request_customerMoblie").removeAttr("disabled");
                $("#request_tr1").show();
                $("#request_tr2").show();
                $("#request_tr3").show();
                $("#request_tr4").show();
                $("#request_tr5").show();
                $("#request_tr6").show();
                $("#request_tr7").show();
          //      _this.contactType.setEnable();
            }
        });
    },
    
	customerFormVal:function(json){
		if(json.ChangeType=='10'&&json.HouseStatus!='-1'){
			alert('无法进行此操作，请至房源编辑处编辑房源记录！');
			return false;
		}
		return true;
	},
	formValidator: function(model) {
        var status = true;
        if (model.ChangeType == '10') {
            if (this.scope.currentHouseInfo) {
                if (model.HouseStatus == this.scope.currentHouseInfo.Status) {
                    alert('请求数据与当前数据一致!');
                    return false;
                }
            }
        } else if (model.ChangeType == '20') {
            if (this.requestUser) {
                if (this.requestUser.UserId == this.scope.currentHouseInfo.OprPersonId) {
                    alert('请求数据与当前数据一致!');
                    return false;
                }
            }
        } else {
            if (this.requestUser && this.currentHouseInfo) {
                if ((this.requestUser.UserId == this.scope.currentHouseInfo.OprPersonId) && (model.HouseStatus == this.scope.currentHouseInfo.Status)) {
                    alert('请求数据与当前数据一致!');
                    return false;
                }
            }
        }
        switch (model.ChangeType) {
            case '10':
                $("#requestChangePersonId").unFormValidator(true);
                $("#request_customerName").unFormValidator(true);
                $("#request_customerPhone").unFormValidator(true);
                $("#request_customerMoblie").unFormValidator(true);
                status = this.checkCombo() && jQuery.formValidator.pageIsValid(this.id);
				break;
            case '20':
                $("#request_customerName").unFormValidator(false);
                $("#request_customerPhone").unFormValidator(false);
                $("#request_customerMoblie").unFormValidator(false);
                $("#requestChangePersonId").unFormValidator(false);
                status = jQuery.formValidator.pageIsValid(this.id);
                break;
            case '30':
                $("#request_customerName").unFormValidator(false);
                $("#request_customerPhone").unFormValidator(false);
                $("#request_customerMoblie").unFormValidator(false);
                $("#requestChangePersonId").unFormValidator(false);
                status = jQuery.formValidator.pageIsValid(this.id) && this.checkCombo();
                break;
            default:
                status = false;
                break;

        }
        $("#request_customerName").unFormValidator(false);
        $("#request_customerPhone").unFormValidator(false);
        $("#request_customerMoblie").unFormValidator(false);
        $("#requestChangePersonId").unFormValidator(false);
        if (!status) {
            alert('请填写完必填(选)项再进行表单提交！');
        }
				
		if(this.customerFormVal&&status){
			status=this.customerFormVal(model);
		}
		
        return status;

    },
    initComponent: function() {
        Chenxin.UI.ChangeConfirmationForm.superclass.initComponent.call(this);
    }
});

/**
*@description 请求变更表单
*@comboIdPrefix DIV下拉框和提交按钮前缀
*/
Chenxin.FormCommon.houseChangeConfirmationForm = function(comboIdPrefix) {
        return '<table class="tbl_f jqTabWh"><tbody> <tr><th style="width:80px; height:25px;">房源编号：</th><td style="width:340px;" ><input type="text" name="HouseCode" readonly="readonly" class="inputbox_n"/></td></tr><tr><th style="width:80px; height:25px;">地址：</th><td><input type="text" name="Address" readonly="readonly" class="inputbox_n"/></td></tr><tr><th style="width:80px; height:25px;">原所属人员：</th> <td><input type="text" name="AddPerson" class="inputbox_n" readonly="readonly" /></td></tr><tr><th style="width:80px; height:25px;">原所属门店：</th> <td><input type="text" name="AddDepart" class="inputbox_n" readonly="readonly" /></td></tr><tr><th style="width:80px; height:25px;">目前状态：</th> <td><input type="text" name="nowStatus" class="inputbox_n" readonly="readonly" /></td></tr><tr><th style="width:80px; height:25px;">请求方式：</th><td colspan="3"><input value="10" name="ChangeType" id="requestStyle1" type="radio" />变更状态<input value="20"  name="ChangeType" id="requestStyle2" type="radio"/>变更所属人<input checked="checked" value="30" name="ChangeType" id="requestStyle3" type="radio" />变更状态及所属人</td></tr><tr id ="request_tr1" ><th id="request_1"  style="width:80px; height:25px;">状态变更：</th><td style="width:250px;"><div style="z-index: 897; margin-right: 5px;" id="' + comboIdPrefix + '_requestChangeStyle"  class="SelectWrapSingle"></div></td></tr><tr id ="request_tr2"><th id="request_2" >目标人工号：</th><td ><div style="float:left"><input disabled="disabled" name="RequestUserId" id="requestChangePersonId"  maxlength="20" class="inputbox_n" type="text" /></div><div style="float:left" id="requestChangePersonIdTip"/></td></tr><tr id ="request_tr3"><th id="request_3" >目标人姓名：</th><td><input name="RequestUser" class="inputbox_n" readonly="readonly" type="text" id ="request_name" /></td></tr><tr id ="request_tr4"><th id="request_4" >目标人门店：</th> <td><input readonly="readonly" class="inputbox_n" type="text" id ="request_department" name="RequestDepart" /></td></tr><tr id ="request_tr5"><th id="request_5" >客户姓名：</th><td ><div style="float:left"><input disabled="disabled" name="request_customerName" id="request_customerName"  maxlength="20" class="inputbox_n" type="text" /></div><div style="float:left" id="request_customerNameTip"/></td></tr>  <tr id="request_tr6"><th id="request_6">客户手机：</th><td ><div style="float:left"><input disabled="disabled" name="request_customerMoblie" id="request_customerMoblie"  maxlength="20" class="inputbox_n" type="text" /></div><div style="float:left" id="request_customerMoblieTip"/></td></tr><tr id ="request_tr7"><th id="request_7">客户电话：</th><td ><div style="float:left"><input disabled="disabled" name="request_customerPhone" id="request_customerPhone" size="50" maxlength="50" class="inputbox_n" type="text" /></div><div style="float:left" id="request_customerPhoneTip"/></td></tr>   <tr><th></th><td><span style=\"color:red; font-weight:bold;\">备注将会显示在跟单信息中，请勿将电话写入备注！</span><td> </tr><tr><th valign="top"  >请求说明：</th><td colspan="3"><textarea name="Remarks" onkeypress="if (this.value.length>=200){event.returnValue=false}"  onchange="if (this.value.length>=200){this.value=this.value.substr(0, 200)}" id ="requestChangeTxtSummary" cols="40" rows="3" class="textarea_n" style="width: 450px; height: 50px; padding:5px;"></textarea></td></tr></tbody> </table>'
};